package org.come.login;

import com.tool.tcpimg.UIUtils;
import org.come.bean.LoginInfo;
import org.come.bean.LoginRoleInfo;
import org.come.entity.RegionResult;
import org.come.test.Main;
import org.come.until.AnalysisString;
import org.come.until.CutButtonImage;
import org.come.view.View;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class AreaView extends View {
  private SpriteBtn btnEnterBtn;
  private SpriteBtn btnNewxq;
  private SpriteBtn btnEstablished;
  private final JLabel labNetwork;
  private final JLabel labSelection;
  private JLabel labmorenS;
  private JLabel labEstablished;
  private SpriteBtn[] btnSectors;
  public static SpriteBtn[] btnSectorcl;
  private JLabel[] labAreacodes;
  private SpriteBtn[] btnSectorss;
  private SpriteBtn[] btnAreaBottom;
  private JLabel[] labAreacodess;
  private String[] aramBackgroundImagesUrl = { "B5FE7FCD.png", "BFBE1987.png", "53683A80.png", "C2C6E7CC.png", "F591D605.png" };
  //private String[] aramBtnAreaBottomCoImagesUrl = { "E8192240.was", "F0DD31DD.was", "32F3693D.was", "39C1C229.was" };
  //private SpriteBtn areaBottomBtn1;
  //private SpriteBtn areaBottomBtn2;
  //private SpriteBtn areaBottomBtn3;
  //private SpriteBtn areaBottomBtn4;
  private SpriteBtn areaBottomReturnLogin;
  private Integer aramIndex = Integer.valueOf(1);
  private final ImageIcon xqbj;
  private JLabel labMsgTip;
  private BindAccountView accountView;
  private BindAccountTipView accountTipView;
  long time;
  private List<RegionResult> regionResultList;
  private RegionResult area;
  private SpriteBtn[] btnDel;// 删除
  private JLabel[] commonlyUseds;// 常用角色
  private JLabel[] commonlyUsedsIcon;// 常用角色头像
  private JLabel[] commonlyUsedsName;// 常用角色名称
  private JLabel[] commonlyUsedsLvl;// 常用角色等级
  private JLabel[] commonlyUsedsRaceName;// 常用角色种族
  private JLabel[] commonlyAreaName;// 常用大区名称

  public AreaView(List<RegionResult> regionResultList, LoginInfo loginInfo, LoginJpanel loginJpanel) {
    setBounds(0, 0, 1024, 720);
    this.xqbj = new ImageIcon("resource/NewUi/选区.png");

    int b_x = 50;
    int b_y = 435;

    drawSectors(regionResultList, loginJpanel);
    this.labSelection = new JLabel();
    this.labSelection.setBounds(137, 440, 76, 26);
    this.labSelection.setForeground(Color.white);
    this.labSelection.setFont(new Font("楷体", 0, 16));
    this.labSelection.setHorizontalAlignment(0);
    this.labSelection.setText("");
    add(this.labSelection);
    this.labNetwork = new JLabel();
    this.labNetwork.setBounds(380, 440, 63, 26);
    this.labNetwork.setForeground(Color.white);
    this.labNetwork.setFont(new Font("楷体", 0, 16));
    this.labNetwork.setHorizontalAlignment(0);
    this.labNetwork.setText("");
    add(this.labNetwork);
    this.labMsgTip = new JLabel();
    this.labMsgTip.setBounds(45, 406, 300, 30);
    this.labMsgTip.setFont(new Font("楷体", 1, 16));
    this.labMsgTip.setForeground(Color.red);
    add(this.labMsgTip);

    addCommonlyUsedRoles(loginJpanel, loginInfo);
  }

  public void addCommonlyUsedRoles(LoginJpanel loginJpanel, LoginInfo loginInfo){
    if (commonlyUseds != null) {
      for (int i = 0; i < commonlyUseds.length; i++) {
        this.remove(btnDel[i]);
        this.remove(commonlyUseds[i]);
        this.remove(commonlyUsedsIcon[i]);
        this.remove(commonlyUsedsName[i]);
        this.remove(commonlyUsedsLvl[i]);
        this.remove(commonlyUsedsRaceName[i]);
        this.remove(commonlyAreaName[i]);
      }
      commonlyUseds = null;
      commonlyUsedsIcon = null;
      commonlyUsedsName = null;
      commonlyUsedsLvl = null;
      commonlyUsedsRaceName = null;
      commonlyAreaName = null;
      btnDel = null;
    }

    List<LoginRoleInfo> roleInfos = loginInfo.getLoginRoleInfos();
    if (roleInfos != null) {
      if (roleInfos.size() > 0) {
        commonlyUseds = new JLabel[roleInfos.size()];
        commonlyUsedsIcon = new JLabel[roleInfos.size()];
        commonlyUsedsName = new JLabel[roleInfos.size()];
        commonlyUsedsLvl = new JLabel[roleInfos.size()];
        commonlyUsedsRaceName = new JLabel[roleInfos.size()];
        commonlyAreaName = new JLabel[roleInfos.size()];
        btnDel = new SpriteBtn[roleInfos.size()];
        for (int i = 0; i < roleInfos.size() && i < 5; i++) {
          LoginRoleInfo loginRoleInfo = roleInfos.get(i);
          int y = i * 90;
          this.commonlyUsedsIcon[i] = new JLabel();
          this.commonlyUsedsIcon[i].setBounds(808, 140 + y, 60, 60);
          this.commonlyUsedsIcon[i].setIcon(CutButtonImage.getImage("img/head/s"+loginRoleInfo.getRaceId()+".png", 60, 60));
          this.add(this.commonlyUsedsIcon[i]);

          this.commonlyUsedsName[i] = new JLabel(loginRoleInfo.getRoleName());
          this.commonlyUsedsName[i].setBounds(875, 140 + y, 180, 24);
          this.commonlyUsedsName[i].setForeground(Color.BLACK);
          this.commonlyUsedsName[i].setFont(UIUtils.TEXT_FONT33);
          this.add(this.commonlyUsedsName[i]);
          this.commonlyUsedsLvl[i] = new JLabel(AnalysisString.lvl(loginRoleInfo.getRoleLvl()) + "级");
          this.commonlyUsedsLvl[i].setBounds(875, 160 + y, 90, 24);
          this.commonlyUsedsLvl[i].setForeground(Color.BLACK);
          this.commonlyUsedsLvl[i].setFont(UIUtils.TEXT_FONT15);
          this.add(this.commonlyUsedsLvl[i]);

          this.commonlyUsedsRaceName[i] = new JLabel(loginRoleInfo.getRaceName());
          this.commonlyUsedsRaceName[i].setBounds(955, 160 + y, 90, 24);
          this.commonlyUsedsRaceName[i].setForeground(Color.BLACK);
          this.commonlyUsedsRaceName[i].setFont(UIUtils.TEXT_FONT15);
          this.add(this.commonlyUsedsRaceName[i]);

          this.commonlyAreaName[i] = new JLabel(loginRoleInfo.getAreaName());
          this.commonlyAreaName[i].setBounds(925, 180 + y, 90, 24);
          this.commonlyAreaName[i].setForeground(Color.DARK_GRAY);
          this.commonlyAreaName[i].setFont(UIUtils.TEXT_NAME_FONT);
          this.add(this.commonlyAreaName[i]);

          this.btnDel[i] = new SpriteBtn("resource/NewUi/X",180,0,false);
          this.btnDel[i].setBounds(180,0,16,23);
          this.btnDel[i].addMouseListener(new AreaMouslisten(this.btnDel[i], loginJpanel, loginRoleInfo,1));
          this.commonlyUseds[i] = new JLabel(){
            protected void paintComponent(Graphics g) {
              super.paintComponent(g);
              if (btnDel != null) {
                for (int i = 0; i < btnDel.length; i++) {
                  btnDel[i].draw(g);
                }
              }
            }
          };
          commonlyUseds[i].add(this.btnDel[i]);
          this.commonlyUseds[i].setIcon(new ImageIcon("resource/NewUi/常用角色.png"));
          this.commonlyUseds[i].setBounds(800, 134 + y, 201, 84);
          this.commonlyUseds[i].addMouseListener(new AreaMouslisten(null, loginJpanel, loginRoleInfo, 0));
          this.add(this.commonlyUseds[i]);
        }
      }
    }
  }

  public void delCommonlyUsedRoles(LoginJpanel loginJpanel,LoginRoleInfo loginRoleInfo) {//移除角色
    LoginInfo loginInfo = Main.frame.getLoginInfo();
    loginInfo.delCommonlyUsedRoles(loginRoleInfo);
    addCommonlyUsedRoles(loginJpanel, loginInfo);
  }

  public void drawSectors(List<RegionResult> regionResultList, LoginJpanel loginJpanel) {
    if (this.btnSectors != null) {
      for (int i = 0; i < this.btnSectors.length; i++) {
        remove(this.btnSectors[i]);
        remove(this.labAreacodes[i]);

        remove(this.btnAreaBottom[i]);
        remove(this.btnSectors[i]);
      }
    }
    this.btnSectors = new SpriteBtn[regionResultList.size()];
    this.labAreacodes = new JLabel[this.btnSectors.length];

    this.btnSectorss = new SpriteBtn[regionResultList.size()];
    this.labAreacodess = new JLabel[this.btnSectorss.length];
    this.btnAreaBottom = new SpriteBtn[regionResultList.size()];
    area = regionResultList.get(0);
    for (int i = 0; i < this.btnSectors.length; i++) {
      int x = i % 4 * 185;
      int y = i / 4 * 80 + 30;
      //新区
      if("3".equals(regionResultList.get(i).getIfNew())) {
    	  this.btnSectors[i] = new SpriteBtn("resource/NewUi/regions-border1", 80 + x, 220 + y, false);
    	  this.btnNewxq = new SpriteBtn("resource/NewUi/regions-icon", 60 + x, 220 + y, false);
    	  this.btnNewxq.setBounds(60 + x, 220 + y, 20, 58);
    	  add(this.btnNewxq);
      }else if("2".equals(regionResultList.get(i).getIfNew())) {
    	  this.btnSectors[i] = new SpriteBtn("resource/NewUi/regions-region-bglc.was", 80 + x, 220 + y, false);
      }else {
    	  this.btnSectors[i] = new SpriteBtn("resource/NewUi/regions-region-bgwh.was", 80 + x, 220 + y, false);
      }
      this.btnSectors[i].setBounds(80 + x, 220 + y, 69, 58);
      this.btnSectors[i].addMouseListener(new AreaMouslisten(((RegionResult)regionResultList.get(i)).getRA_ID(), this.btnSectors[i], loginJpanel, (RegionResult)regionResultList.get(i)));
      add(this.btnSectors[i]);



      this.labAreacodes[i] = new JLabel();
      this.labAreacodes[i].setForeground(Color.black);
      this.labAreacodes[i].setFont(new Font("楷体", 1, 14));
      this.labAreacodes[i].setText(((RegionResult)regionResultList.get(i)).getRA_NAME());
      this.labAreacodes[i].setHorizontalAlignment(0);
      this.labAreacodes[i].setBounds(91 + x, 207+y, 69, 58);
      add(this.labAreacodes[i]);

    }
  }


  @Override
  protected void paintComponent(Graphics g) {
    g.drawImage(this.xqbj.getImage(), 0, 0, 1024, 720, null);
    this.time += 12L;
    for (int i = 0; i < this.btnSectors.length; i++) {
      //状态条
      //this.btnAreaBottom[i].draw(g);
      //连续播放动画定义 this.btnSectors[i].getSprite().updateToTime(this.time, 0); 屏蔽就是按键动画效果
      this.btnSectors[i].getSprite().updateToTime(this.time, 0);
      this.btnSectors[i].draw(g);
      this.btnNewxq.draw(g);
    }
//    this.areaBottomBtn4.draw(g);
//    this.areaBottomBtn3.draw(g);
//    this.areaBottomBtn2.draw(g);
//    this.areaBottomBtn1.draw(g);
//    this.areaBottomReturnLogin.draw(g);
  }

  public BindAccountView getAccountView() {
    return this.accountView;
  }

  public void setAccountView(BindAccountView accountView) {
    this.accountView = accountView;
  }

  public BindAccountTipView getAccountTipView(){
    return this.accountTipView;
  }

  public void setAccountTipView(BindAccountTipView accountTipView) {
    this.accountTipView = accountTipView;
  }

  public JLabel getLabMsgTip() {
    return this.labMsgTip;
  }

  public void setLabMsgTip(JLabel labMsgTip) {
    this.labMsgTip = labMsgTip;
  }

  public List<RegionResult> getRegionResultList() {
    return this.regionResultList;
  }

  public void setRegionResultList(List<RegionResult> regionResultList) {
    this.regionResultList = regionResultList;
  }

  public void setArea(RegionResult area) {
    this.area = area;
  }

  public RegionResult getArea() {
    return area;
  }

  public JLabel getLabNetwork() {
    return labNetwork;
  }
}
